package com.hxzy.controller.back;

import cn.hutool.crypto.digest.BCrypt;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hxzy.common.annons.Shop;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.PageVO;
import com.hxzy.common.vo.R;
import com.hxzy.config.WaimaiThreadLocal;
import com.hxzy.dto.back.EmployeeAddDTO;
import com.hxzy.dto.back.EmployeeEditDTO;
import com.hxzy.entity.Employee;
import com.hxzy.vo.back.AdminLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.hxzy.dto.back.MerchandiseClassSearchDTO;
import com.hxzy.entity.MerchandiseClass;
import com.hxzy.service.MerchandiseClassService;

import javax.validation.Valid;
import java.lang.Long;

/**
* @Author 商品分类表
* @Description TODO
* @Date 2023/4/6 17:11
* @Version 1.0
*/
@Api(tags = "商品分类表API")
@RestController
@RequestMapping(value = "/api/merchandiseclass")
public class MerchandiseClassController  extends BaseController {


    @Autowired
    private MerchandiseClassService  MerchandiseClassService;

    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R<PageVO<MerchandiseClass>>  search(MerchandiseClassSearchDTO  searchDTO){
       //如果是商家，必须要赋值，只允许查询自己的数据
        AdminLoginVO adminLoginVO = WaimaiThreadLocal.adminThreadLocal.get();
        //如果是商家
        if(adminLoginVO.getAccountType()==2){
           searchDTO.setMId(adminLoginVO.getId());
        }

        //开启分页
        PageHelper.startPage(searchDTO.getPage(),searchDTO.getSize());
        //普通查询
        List<MerchandiseClass> arr=this.MerchandiseClassService.search(searchDTO);
        //转换成自己的对象
        return super.pageToPageVO(arr);
    }

    @Shop
    @ApiOperation(value = "新增")
    @PostMapping
    public R  insert(@RequestBody @Valid MerchandiseClass merchandiseClass) {
        AdminLoginVO adminLoginVO = WaimaiThreadLocal.adminThreadLocal.get();

        merchandiseClass.setCreateTime(new Date());
        merchandiseClass.setCreateBy(adminLoginVO.getName());
        merchandiseClass.setMId(adminLoginVO.getId());

        //保存
        int count = this.MerchandiseClassService.insert(merchandiseClass);
        return super.toR(count);
    }

    @Shop
    @ApiOperation(value = "修改")
    @PutMapping
    public R  update(@RequestBody @Valid MerchandiseClass editDTO) {
        AdminLoginVO adminLoginVO = WaimaiThreadLocal.adminThreadLocal.get();


        MerchandiseClass db=this.MerchandiseClassService.selectByPrimaryKey(editDTO.getId());
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        db.setName(editDTO.getName());
        db.setPicture(editDTO.getPicture());
        editDTO.setUpdateTime(new Date());


        db.setUpdateBy(adminLoginVO.getName());

        //执行
        int count = this.MerchandiseClassService.updateByPrimaryKeySelective(editDTO);

        return  super.toR(count);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R<MerchandiseClass> findById(@PathVariable(value = "id") Long id){
        MerchandiseClass db=this.MerchandiseClassService.selectByPrimaryKey(id);
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(db);
    }

}
